import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
} from '@nestjs/common';
import { FilmPathRelationalService } from './film_path_relational.service';
import { PathListService } from 'src/path-list/path-list.service';
import { FilmListService } from 'src/film-list/film-list.service';
import { CreateFilmPathRelationalDto } from './dto/create-film_path_relational.dto';
import { Public } from 'src/common/decorator/public.decorator';
import { UpdateFilmPathRelationalDto } from './dto/update-film_path_relational.dto';

@Controller('film-path-relational')
export class FilmPathRelationalController {
  constructor(
    private readonly filmPathRelationalService: FilmPathRelationalService,
    private readonly pathListService: PathListService,
    private readonly filmListService: FilmListService,
  ) {}

  @Post('createRelation')
  async createRelation(
    @Body() createFilmPathRelationalDto: CreateFilmPathRelationalDto,
  ) {
    try {
      // 查询轨道和场景是否存在
      const pathResult = await this.pathListService.findOne(
        createFilmPathRelationalDto.path_id,
      );

      const sceneResult = await this.filmListService.getFilmItemById(
        createFilmPathRelationalDto.film_id,
      );

      // 确保轨道和场景存在，然后创建关系
      if (pathResult && sceneResult) {
        if (
          !(await this.filmPathRelationalService.existsByFilmIdAndPathId(
            createFilmPathRelationalDto.film_id,
            createFilmPathRelationalDto.path_id,
          ))
        ) {
          await this.filmPathRelationalService.createRelation(
            createFilmPathRelationalDto,
          );
          return 'Relation created successfully';
        } else {
          return 'Relation already exists';
        }
      } else {
        throw new BadRequestException('Invalid path or scene id');
      }
    } catch (error) {
      throw new BadRequestException('Failed to create relation');
    }
  }

  @Get('getAll')
  async getAll() {
    return this.filmPathRelationalService.getAll();
  }

  @Get('getPathByPathIdsAndTypeByFilmId/:filmId/:fileType')
  getPathByPathIdsAndTypeByFilmId(
    @Param('filmId') filmId: string,
    @Param('fileType') fileType: string,
  ) {
    return this.filmPathRelationalService.getPathByPathIdsAndTypeByFilmId(
      filmId,
      fileType,
    );
  }

  @Public()
  @Get('getPathByPathIdsByFilmId/:filmId')
  getPathByPathIdsByFilmId(@Param('filmId') filmId: string) {
    return this.filmPathRelationalService.getPathByPathIdsByFilmId(filmId);
  }

  @Get('getRelationById/:id')
  getRelationByid(@Param('id') id: string) {
    return this.filmPathRelationalService.getRelationByid(id);
  }

  @Get('findByFilmId/:filmId')
  findByFilmId(@Param('filmId') filmId: string) {
    return this.filmPathRelationalService.findByFilmId(filmId);
  }

  @Patch('updateRelationById/:id')
  updateRelationById(
    @Param('id') id: string,
    @Body() updateFilmPathRelationalDto: UpdateFilmPathRelationalDto,
  ) {
    return this.filmPathRelationalService.updateRelationById(
      id,
      updateFilmPathRelationalDto,
    );
  }

  @Delete('deleteRelationById/:id')
  deleteRelationById(@Param('id') id: string) {
    return this.filmPathRelationalService.deleteRelationById(id);
  }

  @Delete('deleteRelationByFilmIdByPathId/:pathId/:filmId')
  deleteRelationByFilmIdByPathId(
    @Param('pathId') pathId: string,
    @Param('filmId') filmId: string,
  ) {
    return this.filmPathRelationalService.deleteRelationByFilmIdByPathId(
      pathId,
      filmId,
    );
  }
}
